
//
//  ApplePayController.m
//  TouchTest
//
//  Created by fengxijun on 2/19/16.
//  Copyright © 2016 goappbox. All rights reserved.
//

#import "ApplePayController.h"
#import <PassKit/PassKit.h>

@interface ApplePayController ()
<
    PKPaymentAuthorizationViewControllerDelegate
>
@end

@implementation ApplePayController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(60, 100, 200, 50);
    btn.center = self.view.center;
    [btn setTitle:@"Apple Pay" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//    [btn setBackgroundImage:[UIImage imageNamed:@"ApplePayBTN_64pt__whiteLine_textLogo_"] forState:UIControlStateNormal];
    
    [btn addTarget:self action:@selector(applePay) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}

- (void) applePay
{
    if (![PKPaymentAuthorizationViewController canMakePayments]) {
        NSLog(@"不支持Apple Pay");
        return;
    }
    
    PKPaymentRequest *request = [[PKPaymentRequest alloc] init];
    
    PKPaymentSummaryItem *widget1 = [PKPaymentSummaryItem summaryItemWithLabel:@"原价" amount:[NSDecimalNumber decimalNumberWithMantissa:10 exponent:-2 isNegative:NO]];
    PKPaymentSummaryItem *widget2 = [PKPaymentSummaryItem summaryItemWithLabel:@"折扣" amount:[NSDecimalNumber decimalNumberWithMantissa:1 exponent:-2 isNegative:YES]];
    PKShippingMethod *widget3 = [PKShippingMethod summaryItemWithLabel:@"配送费" amount:[NSDecimalNumber decimalNumberWithString:@"0"]];
    widget3.identifier = @"哈哈";
    widget3.detail = @"24小时配送";
    //    widget4.identifier = @"";

    NSDecimalNumber *totalAmount = [NSDecimalNumber zero];
    totalAmount = [totalAmount decimalNumberByAdding:widget1.amount];
    totalAmount = [totalAmount decimalNumberByAdding:widget2.amount];
    totalAmount = [totalAmount decimalNumberByAdding:widget3.amount];
    
    PKPaymentSummaryItem *widget4 = [PKPaymentSummaryItem summaryItemWithLabel:@"成都一指游科技有限公司" amount:totalAmount type:PKPaymentSummaryItemTypeFinal];

    request.paymentSummaryItems = @[widget1,widget2,widget3,widget4];
    
    request.countryCode = @"CN";
    request.currencyCode = @"CHW";
    request.supportedNetworks = @[PKPaymentNetworkChinaUnionPay,PKPaymentNetworkVisa,PKPaymentNetworkMasterCard];   //设置支持的卡种类
    request.merchantIdentifier = @"com.goappbox.TouchTest";
    request.merchantCapabilities = PKMerchantCapability3DS | PKMerchantCapabilityDebit | PKMerchantCapabilityCredit;
    request.requiredBillingAddressFields = PKAddressFieldEmail | PKAddressFieldPostalAddress;
    PKPaymentAuthorizationViewController *paymentController = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
    paymentController.delegate = self;
    if (!paymentController) {
        NSLog(@"支付出问题了!");
    } else {
        [self presentViewController:paymentController animated:YES completion:^{
            //
        }];
    }
}

- (void) paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectPaymentMethod:(PKPaymentMethod *)paymentMethod completion:(void (^)(NSArray<PKPaymentSummaryItem *> * _Nonnull))completion
{
}

- (void) paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectShippingMethod:(PKShippingMethod *)shippingMethod completion:(void (^)(PKPaymentAuthorizationStatus, NSArray<PKPaymentSummaryItem *> * _Nonnull))completion
{
}

- (void) paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus))completion
{
//    NSLog(@"payment was authorized : %@",payment);
//    BOOL asyncSuccessful = NO;
//    if (asyncSuccessful) {
//        completion(PKPaymentAuthorizationStatusSuccess);
//        // do something to let the user know the status
//        NSLog(@"支付成功");
//    } else {
//        completion(PKPaymentAuthorizationStatusFailure);
//        // do something to let the user know the status
//        NSLog(@"支付失败");
//    }
    NSLog(@"payment :[%@ : %@]",[NSJSONSerialization JSONObjectWithData:[payment.token paymentData] options:NSJSONReadingAllowFragments error:nil],payment.shippingMethod);
    completion(PKPaymentAuthorizationStatusSuccess);
}

- (void) paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller
{
    [controller dismissViewControllerAnimated:YES completion:nil];
}

@end
